<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Config;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="分类列表")
 */
class Category extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Company();
        $this->connect_todatabase();
    }

 
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
        
            $count = Db::connect('tmp')->table('category')
                ->where($where)
                ->where("deleted_at",NULL)
                ->count();
            $list = Db::connect('tmp')->table('category')
                ->where($where)
                ->where("deleted_at",NULL)
                ->page($page, $limit)
                ->order("sort asc")
                ->select();
            foreach($list as $k=>$v){
              
                $list[$k] = $v;
            }
         
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post["name"])){
                $this->error("名称不能为空");
            }
            $data = [
                "name"=>$post["name"],
                "created_at"=>date("Y-m-d H:i:s",time())
            ];
            try {
                Db::connect('tmp')->table('category')->insert($data);

            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $this->success('保存成功');
        }
        return $this->fetch();
      
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post["name"])){
                $this->error("名称不能为空");
            }
            $data = [
                "name"=>$post["name"],
                "updated_at"=>date("Y-m-d H:i:s",time())
            ];
            try {
                Db::connect('tmp')->table('category')->where("id",$id)->update($data);
                
            } catch (\Exception $e) {
                $this->error('保存失败',$e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $row = Db::connect('tmp')->table('category')->where("id",$id)->find();
            $this->assign('row', $row);
            return $this->fetch();
        }
   
    }


     /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = Db::connect('tmp')->table('category')->where('id', $id)->find();
        if(empty($row)){
            $this->error('数据不存在');
        }
        try {
            $save = Db::connect('tmp')->table('category')->where('id', $id)->update(["deleted_at"=>date("Y-m-d H:i:s",time())]);
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
    * @NodeAnotation(title="属性修改")
    */
    public function modify()
    {
        $post = $this->request->post();
        $rule = [
            'id|ID'    => 'require',
            'field|字段' => 'require',
            'value|值'  => 'require',
        ];
        $this->validate($post, $rule);
        $row = Db::connect('tmp')->table('category')->where('id', $post["id"])->find();
        if (!$row) {
            $this->error('数据不存在');
        }
        if (!in_array($post['field'], $this->allowModifyFields)) {
            $this->error('该字段不允许修改：' . $post['field']);
        }
        try {
            Db::connect('tmp')->table('category')->where('id', $post['id'])->update([$post['field']=>$post['value']]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

  
    
}